#!/usr/bin/perl -w
#Task D (4 points)
#The file IGF.fasta is provided along with the assignment instructions. This file 
#contains the DNA sequence of a human growth factor gene. Write a program that reads 
#the file and stores the sequence in a scalar variable (excluding the header). Next, 
#the program should use the substr function to extract a substring from the DNA sequence. 
#Finally, the string extracted should be printed to a new file. Hint: each line in 
#IGF.fasta ends with a newline character
#
# Open the input file
if (!open(MY_HANDLE, "IGF.fasta")){ 
	die "Could not open IGF.fasta"; 
}
#
#Initialize the long string that will contain the whole DNA sequence stored in the file
$DNA = "";
#
# Read the first header line
$fileLine = <MY_HANDLE>;
#
# Loop through all the remaining lines
while($fileLine = <MY_HANDLE>){
	# remove the newline at the end of the line
	 chomp($fileLine);
	 # concatenate the string DNA with each line
	 $DNA = $DNA.$fileLine;
}
#Close the input file
close(MY_HANDLE);
#
# Extract the substring starting at index 34 and 25 characters long
$str = substr $DNA, 34, 25;

print("The substring of the DNA sequence stating at position 34 and length 25 is:\n");
print("$str");

#
# Open the output wile for writing
if(!open(WRITE_HANDLE, ">newFile.txt")){ 
	die "Could not open newFile.txt for writing"; 
} 
#
# Write the substring to the output file "newFile.txt"
print(WRITE_HANDLE "$str"); 
# Close the output file using its handle.
close(WRITE_HANDLE);

